import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.AbstractCollection;
import java.util.ArrayList;


public abstract class DataSaveLoad {
		
		@SuppressWarnings("unchecked")
		public static void load(){
			FileInputStream fis;
			try {
				fis = new FileInputStream("databases.d");
				ObjectInputStream in = new ObjectInputStream(fis);
				UserDatabase.adminList = (ArrayList<SystemAdmin>) in.readObject();
				UserDatabase.patientList = (ArrayList<Patient>) in.readObject();
				UserDatabase.doctorList = (ArrayList<Doctor>) in.readObject();
				UserDatabase.pharmList = (ArrayList<Pharmacist>) in.readObject();
				UserDatabase.nurseList = (ArrayList<Nurse>) in.readObject();
				MedicalRecordDatabase.recordsList = 
					(AbstractCollection<MedicalRecord>) in.readObject();
				//Added in by Matt.
				ProcedureDatabase.PrescriptionList = 
					(ArrayList<Prescription>) in.readObject();
				ProcedureDatabase.LabworkList = 
					(ArrayList<Labwork>) in.readObject();

				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		public static void save(){
			try {
				File data = new File("databases.d");
				data.createNewFile();
				FileOutputStream fos = new FileOutputStream(data);
				ObjectOutputStream out = new ObjectOutputStream(fos);
				out.writeObject(UserDatabase.adminList);
				out.writeObject(UserDatabase.patientList);
				out.writeObject(UserDatabase.doctorList);
				out.writeObject(UserDatabase.pharmList);
				out.writeObject(UserDatabase.nurseList);
				out.writeObject(MedicalRecordDatabase.recordsList);
				//Added in by Matt.
				out.writeObject(ProcedureDatabase.PrescriptionList);
				out.writeObject(ProcedureDatabase.LabworkList);
				
				out.close();
				out.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//For the code audit.
			finally{
				
			}
			
		}
}
